package com.thudojsc.canvasengine.entity;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.text.TextPaint;

public class Text extends Node {
	private final static float DEFAULT_TEXT_SIZE = 14.0f; //Pixels
	
	private String mText;
	
	private int mCaretBottom;
	
	public Text(final String pText) {
		super(0,0);
		
		this.mPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
		
		mPaint.setTextAlign(Align.LEFT);
		mPaint.setDither(true);
		mPaint.setFilterBitmap(true);
		mPaint.setTextSize(DEFAULT_TEXT_SIZE);
		
		setText(pText);
	}
	
	
	/*Typeface*/
	public void setTypeface(final Typeface pTypeface) {
		mPaint.setTypeface(pTypeface);
		
		onMeasure();
	}
	
	public Typeface getTypeface() {
		return mPaint.getTypeface();
	}
	
	/*Style*/
	public void setStype(final Style pStyle) {
		mPaint.setStyle(pStyle);
		
		onMeasure();
	}
	
	public Style getStyle() {
		return mPaint.getStyle();
	}
	
	/*Text*/
	public void setText(final String pText) {
		this.mText = pText;
		onMeasure();
	}
	
	private void onMeasure() {
		final Rect boundText = new Rect();
		mPaint.getTextBounds(mText, 0, mText.length(), boundText);
		
		mWidth = boundText.right - boundText.left;
		mHeight = boundText.bottom - boundText.top;
		
		mCaretBottom = boundText.bottom;
	}
	
	public String getText() {
		return this.mText;
	}
	
	/*Text Size*/
	public void setTextSize(final float pTextSize) {
		mPaint.setTextSize(pTextSize);
		
		onMeasure();
	}
	
	public float getTextSize() {
		return mPaint.getTextSize();
	}
	
	@Override
	protected void onDraw(Canvas pCanvas) {
		final Paint p = new Paint();
		p.setColor(Color.BLUE);
		pCanvas.drawRect(0, 0, mWidth, mHeight, p);
		pCanvas.drawText(mText, 0, mHeight - mCaretBottom, mPaint);
	}
}
